package com.xiaoban.handler;

import com.xiaoban.pojo.LoginSyUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 小班哥
 * @version 1.0
 * @date 2024/3/18 21:07
 * @description 自定义权限校验方法
 */
@Component("ex")
public class SGExpressionRoot {

    /**
     *
     * @param authority
     * @return
     */
    public boolean hasAuthority(String authority){
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginSyUser principal = (LoginSyUser) authentication.getPrincipal();
        List<String> permissionsList = principal.getPermissions();
        //判断用户权限集合中是否拥有authority
        return permissionsList.contains(authority);
    }
}
